Spring Boot入门

Author Avatar
Damon Lee 7月 31, 2018
  • 在其它设备中阅读本文章

前言

我为什么会选择用Spring Boot来做Web项目,主要就是因为它支持yaml格式的配置,配合各种stater插件,让开发者终于不再为眼花缭乱的xml配置文件而头疼了。再加上其内嵌Tomcat,可以免去一些新手配置环境的烦恼。

后续我会写一系列关于Spring Cloud的文章,所以今天的Spring Boot教程是后面的Spring Cloud的基础。

关于Spring Boot 官网链接

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。主要具有如下的特点:
  • 嵌入的Tomcat,无需部署WAR文件。
  • 简化Maven配置。
  • 自动配置Spring.
  • 配置多种启动环境(prod,dev,test等)。
  • yaml配置。

创建项目

打开Idea,新建一个Maven项目,填好GroupId(一般是公司或个人域名倒过来写)和ArtifactId(项目标识)以及版本号。然后打开项目目录下的pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.lizhenghlh</groupId>
<artifactId>hellospringboot</artifactId>
<version>1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-boot.version>1.5.12.RELEASE</spring-boot.version>
<mybatisplus.spring.boot.version>1.0.5</mybatisplus.spring.boot.version>
<mybatisplus.version>2.3</mybatisplus.version>
<mysql.version>8.0.11</mysql.version>
<druid.version>1.1.3</druid.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>

</project>
然后创建HelloApplication
1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

/**
* Created by zhenglee on 2018/7/31.
*/
@SpringBootApplication
public class HelloApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(HelloApplication.class).web(true).run(args);
}
}

创建HelloController

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hello")
public class HelloController {

@GetMapping
public String hello() {
return "Hello, Spring Boot!";
}
}
这时候你需要在application-dev.yml中配置好你的数据库配置,包括url,username,password,如果配置正确这时候就可以启动项目了,启动前先创建启动配置,如下图:

1

此时在浏览器中输入http://localhost:8080/hello ,就可以看到“Hello, Spring Boot!”字样代表项目启动成功。
当然仅仅显示一个“Hello, Spring Boot!”似乎太简单了,下面我继续创建一个BookController用来管理书籍,其中Book的实体,Dao,Service我就不放代码了,有兴趣的可以通过我的Github链接来获取项目的源码,BookController的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import io.github.lizhenghlh.entity.Book;
import io.github.lizhenghlh.entity.Response;
import io.github.lizhenghlh.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("books")
public class BookController {

@Autowired
private BookService bookService;

@PostMapping
public Response add(Book book) {
bookService.insert(book);
return Response.build().put("data", book).success();
}

@DeleteMapping
public Response delete(Long id) {
bookService.deleteById(id);
return Response.build().success("删除成功");
}

@PutMapping
public Response update(Book book) {
final Book exist = bookService.selectById(book.getId());
if (null == exist) {
return Response.build().fail("不存在此记录");
}
bookService.updateById(book);
return Response.build().put("data", book).success();
}

@GetMapping
public Response list() {
Map<String, Object> result = new HashMap<>();
result.put("data", bookService.selectList(null));
return Response.build(result).success();
}

@GetMapping("/{id}")
public Response findById(@PathVariable Long id) {
final Book book = bookService.selectById(id);
if (null == book) {
return Response.build().fail("不存在此记录");
}
return Response.build().put("data", book).success();
}

}
然后重启项目,打开postman或者paw,新建几个测试,一个书籍的增删改查就算做完了。这个项目很简单,没有做过多的严格处理,但是用来理解Web项目肯定是足够了。

2

3

4

5

6

最后在放下项目的源码链接https://github.com/lizhenghlh/hellospringboot